package com.halden.TRPG.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.halden.TRPG.entity.MessageEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MessageMapper extends BaseMapper<MessageEntity> {
    int createMessageTable (@Param("rid") Long rid);

    @Insert({
            "<script>",
            "insert into room${rid}(uid,username,message,create_time) values ",
            "<foreach collection='messages' item='item' index='index' separator=','>",
            "(#{item.uid}, #{item.username}, #{item.message}, #{item.createTime})",
            "</foreach>",
            "</script>"
    })
    int saveMessages(@Param("rid") Long rid, List<MessageEntity> messages);

    @Select("select * from room${rid} order by id  DESC limit #{start},#{length}")
    List<MessageEntity> getMessages(Long rid, Long start, Long length);

    int deleteMessages(@Param("rid") Long rid);

    @Select("select * from room${rid} order by id")
    List<MessageEntity> getAllMessages(Long rid);
}
